/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：CommonBattle.java <br>
 * 创建时间：2014-2-26 下午6:08:46 <br>
 * 所属项目：GmudEX <br>
 *
 * @author 12548 <br>
 */
package lostland.gmud.exv2.battle.proc.holder;

import java.util.ArrayList;
import java.util.Locale;

import lostland.gmud.exv2.GameMessageHandler;
import lostland.gmud.exv2.GameOverScreen;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.MapScreen;
import lostland.gmud.exv2.battle.BattleScreen;
import lostland.gmud.exv2.data.GmudMap;
import lostland.gmud.exv2.data.IngameData;
import lostland.gmud.exv2.data.Npc;
import lostland.gmud.exv2.ui.EventScreen;
import lostland.gmud.exv2.ui.SimpleNotificationScreen;
import lostland.gmud.exv2.ui.TalkingScreen;
import lostland.gmud.exv2.ui.WinScreen;
import lostland.gmud.exv2.ui.core.YesNoScreen;

/**
 * 类名：CommonBattle <br>
 * 说明：通常战斗结束处理
 *
 * @author 12548
 */
public class CommonBattle extends BattleHolder {

	/*
	 * （非 Javadoc）
	 * @see lostland.gmud.exv2.expand.BattleHolder#updato()
	 */
	@Override
	void updato() {

		switch (battle_stat) {
		case BATTLE_PLAYER1_WIN:
			BattleScreen.getInstance().p2().refresh();

			getGame().getHandler().obtainMessage(GameMessageHandler.MSG_SHOWAD).sendToTarget();

			int expdiff = BattleScreen.getInstance().p2().exp - Gmud.mc.exp;
			if (expdiff > 1000 && Math.random() < 0.3) {
				int expBouns = (expdiff + Gmud.rand.nextInt(1000) - 1000) / 100;
				if (expBouns > 0) {
					Gmud.mc.setExp(Gmud.mc.exp + expBouns);
					new SimpleNotificationScreen(String.format(Locale.getDefault(), "你在战斗中获得了%d点实战经验。", expBouns)).insertToGame();
				}
			}

			if (BattleScreen.getInstance().enemyid == 2 && Gmud.mc.fame < 128) {
				String ts[] = {"我会在地狱里等着你的。", "二十年后又是一条好汉！", "你！"};
				ArrayList<String> a = new ArrayList<>();
				a.add(ts[(int) (Math.random() * ts.length)]);

				Gmud.mc.fame -= BattleScreen.getInstance().p2().fame * 2;

				getGame().data.death_note.add(2);
				getGame().data.stat_kills++;
				new EventScreen(a, false).replaceCurrent();
			} else if (BattleScreen.getInstance().p2().haveFlag(Npc.FLAG_IMPORTANT)) {

				if(BattleScreen.getInstance().p2().haveFlag(Npc.FLAG_DISAPPEAR_AFTER_DIE)) {
					GmudMap.clearEvent(MapScreen.npcPlace);
				}

				new WinScreen(BattleScreen.getInstance().enemyid).replaceCurrent();
			} else {
				if (BattleScreen.getInstance().p2().haveTalent(133)) {
					Gmud.mc.lookingplus--;
				}
				new YesNoScreen(Gmud.mc.name + "这一刀要砍下去吗？") {
					@Override
					protected void onYes() {
						String ts[] = {"我会在地狱里等着你的。", "有的人活着，他已经死了。有。。哎，我还没说完。。。", "二十年后又是一条好汉！",
						"啊！"};
						ArrayList<String> a = new ArrayList<>();
						a.add(ts[(int) (Math.random() * ts.length)]);

						if (Gmud.mc.fame >= 128) {
							if (BattleScreen.getInstance().p2().fame > 0) {
								// 杀一个好人，减双倍声望
								Gmud.mc.fame -= BattleScreen.getInstance().p2().fame * 2;
							} else {
								Gmud.mc.fame -= BattleScreen.getInstance().p2().fame;
							}
						} else {
							if (BattleScreen.getInstance().p2().fame > 0) {
								Gmud.mc.fame -= BattleScreen.getInstance().p2().fame;
							}
						}



						if (Gmud.getNpc(BattleScreen.getInstance().enemyid).haveFlag(Npc.FLAG_BADMAN)
								|| Gmud.getNpc(BattleScreen.getInstance().enemyid).haveFlag(Npc.FLAG_NICEGUY)) {

							GmudMap.clearEvent(IngameData.getInstance().badmanPlace);
							GmudMap.clearEvent(IngameData.getInstance().niceguyPlace);

//								Event.badmanPlace.getMap().setEvent(TalkingScreen.ex, TalkingScreen.ey,
//										-1);
//								Event.badmanPlace.getMap().setWalkable(TalkingScreen.ex,
//										TalkingScreen.ey, GmudMap.MP_WALKABLE);
//								Event.niceguyPlace.getMap().setEvent(TalkingScreen.ex, TalkingScreen.ey,
//										-1);
//								Event.niceguyPlace.getMap().setWalkable(TalkingScreen.ex,
//										TalkingScreen.ey, GmudMap.MP_WALKABLE);
							MapScreen.getInstance().number++;
							if (MapScreen.getInstance().number > 9) {
								MapScreen.getInstance().round++;
								MapScreen.getInstance().number = 0;
							}

							if (Gmud.getNpc(BattleScreen.getInstance().enemyid).haveFlag(Npc.FLAG_BADMAN)) {
//								game.nextBadman = 300 - game.data.game_difficulty * 80;
								IngameData.getInstance().nextBadman = 40;
								IngameData.getInstance().huntingBadman = false;
							} else {
//								game.nextNiceguy = 300 - game.data.game_difficulty * 75;
								IngameData.getInstance().nextNiceguy = 40;
								IngameData.getInstance().huntingNiceguy = false;
							}

							if (Gmud.mc.haveTalent(1012)) // 天赋效果1012
							{
								IngameData.getInstance().nextBadman = 0;
								IngameData.getInstance().nextNiceguy = 0;
							}


							int t = (int) ((96 + Math.random() * 10) * (MapScreen.getInstance().round + MapScreen.getInstance().number));
							int e = t * 4;
							int p = t;

							if ((getGame().data.yellowcal & 2) != 0
									&& Gmud.getNpc(BattleScreen.getInstance().enemyid).haveFlag(Npc.FLAG_BADMAN)) {
								t *= 1.2;
							}

							if (Gmud.getNpc(BattleScreen.getInstance().enemyid).haveFlag(Npc.FLAG_NICEGUY)) // 除善奖励调整
							{
								if ((getGame().data.yellowcal & 2) != 0) {
									t *= 1.2;
								}
								p = (int) (t * 1.5);
								e = p;
							}

							a.add("你被奖励了：" + e + "点实战经验，" + p + "点潜能");
							Gmud.mc.setPotential(Gmud.mc.potential + p);
							Gmud.mc.setExp(Gmud.mc.exp + e);
							getGame().data.stat_hunts++;
						} else {
							getGame().data.death_note.add(BattleScreen.getInstance().p2().id);
							getGame().data.stat_kills++;
							if(BattleScreen.getInstance().p2().haveFlag(Npc.FLAG_DISAPPEAR_AFTER_DIE)) {
								GmudMap.clearEvent(MapScreen.npcPlace);
							}
						}

						EventScreen.ts = new WinScreen(BattleScreen.getInstance().enemyid);
						new EventScreen(a, false).replaceCurrent();
					}

					@Override
					protected void onNo() {
						new WinScreen(BattleScreen.getInstance().enemyid).replaceCurrent();
						Gmud.getNpc(BattleScreen.getInstance().enemyid).refresh();
					}
				}.replaceCurrent();
			}
			break;
		case BATTLE_PLAYER2_WIN:

			if ((BattleScreen.getInstance().p2().fame > 0 && Gmud.mc.fame < 128) ||
					(BattleScreen.getInstance().p2().fame <= 0)) {

				{
					String speak = Math.random() > 0.5 ? "$n，去死吧！" : "$n，为什么要逼我出手？";

					{
						TalkingScreen.ts = new GameOverScreen();
						speak = speak.replace("$n", Gmud.mc.name);
					}

					new TalkingScreen(speak).replaceCurrent();
				}
			} else {
				TalkingScreen.ts = MapScreen.getInstance();
				new TalkingScreen("承让了。").replaceCurrent();
			}
			break;

		case BATTLE_RUNAWAY:
			int i1 = BattleScreen.getInstance().p1().getNewpj();
			int i2 = BattleScreen.getInstance().p2().getNewpj();
			TalkingScreen.ts = MapScreen.getInstance();
			if (Math.abs(i1 - i2) <= 20) {
				new TalkingScreen("咱们棋逢对手，胜负难分，择日再战！").replaceCurrent();
			} else if (i2 > i1) {
				ArrayList<String> ss = new ArrayList<>();
				ss.add("哪里逃！");
				ss.add("有种别跑！");
				ss.add("打不过就跑，真丢人！");
				new TalkingScreen(ss).replaceCurrent();
			} else {
				new TalkingScreen("罢了。").replaceCurrent();
			}

			break;
		}
	}
}
